package main

import (
	"github.com/gin-gonic/gin"
	_ "github.com/lib/pq"
	"net/http"
)

const (
	host     = "localhost"
	port     = 5432
	user     = "postgres"
	password = "13801098189"
	dbname   = "macfamilytree"
)

type company struct {
	id        int
	name      string
	age       int
	address   string
	salary    int
	join_date string
}

func main() {

	//db := function.ConnectDB()
	//function.Query(db)

	// 设置路由信息
	r := setupRouter()
	// 启动服务器并监听 3000 端口
	r.Run(":3000")
	//function.Connect()
	//function.Delete()

}

var db = make(map[string]string)

func setupRouter() *gin.Engine {
	// 初始化 Gin 框架默认实例，该实例包含了路由、中间件以及配置信息
	r := gin.Default()

	// Ping 测试路由
	r.GET("/hi", func(c *gin.Context) {
		c.String(http.StatusOK, "hello world!")
	})

	// 获取用户数据路由
	r.GET("/user/:name", func(c *gin.Context) {
		user := c.Params.ByName("name")
		value, ok := db[user]
		if ok {
			c.JSON(http.StatusOK, gin.H{"user": user, "value": value})
		} else {
			c.JSON(http.StatusOK, gin.H{"user": user, "status": "no value"})
		}
	})

	// 需要 HTTP 基本授权认证的子路由群组设置
	authorized := r.Group("/", gin.BasicAuth(gin.Accounts{
		"foo":  "bar", // 用户名:foo 密码:bar
		"manu": "123", // 用户名:manu 密码:123
	}))

	// 保存用户信息路由
	authorized.POST("admin", func(c *gin.Context) {
		user := c.MustGet(gin.AuthUserKey).(string)

		// 解析并验证 JSON 格式请求数据
		var json struct {
			Value string `json:"value" binding:"required"`
		}

		if c.Bind(&json) == nil {
			db[user] = json.Value
			c.JSON(http.StatusOK, gin.H{"status": "ok"})
		}
	})

	return r
}

//增加操作

//sqlStatement2 := `
//	INSERT INTO company (id,name,age,address,salary,join_date)
//	VALUES ($1,$2,$3,$4,$5,$6)
//	RETURNING id,name,age,address,salary,join_date`
//
//var result2 company
//err = db.QueryRow(sqlStatement2, 7, "liuhuaiyu", 18, "DLNU", 10000, "2022-5-27").Scan(&result2.id, &result2.name, &result2.age, &result2.address, &result2.salary, &result2.join_date)
//if err != nil {
//	panic(err)
//}
//fmt.Println("New record is:", result2)

//删除操作

//sqlStatement3 := `
//DELETE FROM company
//WHERE id=6`
//var result3 company
//row2 := db.QueryRow(sqlStatement3)
//err = row2.Scan(&result3.id, &result3.name, &result3.age, &result3.address, &result3.salary, &result3.join_date)
//switch err {
//case sql.ErrNoRows:
//	fmt.Println("Delete complete!")
//	return
//case nil:
//	fmt.Println(result3)
//default:
//	panic(err)

//修改操作

//sqlStatement4 := `
//UPDATE company
//SET salary='5000', name='test'
//WHERE id=6`
//var result4 company
//row3 := db.QueryRow(sqlStatement4)
//err = row3.Scan(&result4.id, &result4.name, &result4.age, &result4.address, &result4.salary, &result4.join_date)
//switch err {
//case sql.ErrNoRows:
//	fmt.Println("Update complete!")
//	return
//case nil:
//	fmt.Println(result4)
//default:
//	panic(err)
//}

//查询操作

//sqlStatement1 := `SELECT * FROM company WHERE id=$1;`
//var result company
//row := db.QueryRow(sqlStatement1, 4)
//err = row.Scan(&result.id, &result.name, &result.age, &result.address, &result.salary, &result.join_date)
//switch err {
//case sql.ErrNoRows:
//	fmt.Println("No rows were returned!")
//	return
//case nil:
//	fmt.Println(result)
//default:
//	panic(err)
//}

//




//	sqlStatement2 := `
//	INSERT INTO company (id,name,age,address,salary,join_date)
//	VALUES ($1,$2,$3,$4,$5,$6)
//	RETURNING id,name,age,address,salary,join_date`
//	id := 6
//
//	err = db.QueryRow(sqlStatement2, ).Scan(&id)(&result.id, &result.name, &result.age, &result.address, &result.salary, &result.join_date)
//	if err != nil {
//		panic(err)
//	}
//	fmt.Println("New record ID is:", id)
//}

//err = db.QueryRow(sqlStatement2, 6).Scan(&result2)
//if err != nil {
//	panic(err)
//}
//fmt.Println("New record ID is:", result2)

//	sqlStatement := `
//INSERT INTO teacher (id, age)
//VALUES ($1, $2)
//RETURNING id`
//	id := 3
//	err = db.QueryRow(sqlStatement, 3, 19).Scan(&id)
//	if err != nil {
//		panic(err)
//	}
//	fmt.Println("New record ID is:", id)
//